package gui.changeTab;
import gui.TableOption;
import gui.userTab.UserManagementTab;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Table;

/**
 * class DeleteRowsListener
 * listener of deletion operation by user. 
 * calls the DeleteThread.
 * @author michal
 *
 */
public class DeleteRowsListener implements KeyListener {	
	private TableOption tableName;	//table option
	private TableCursor cursor;	//cursor
	private Table table;	//current table
	private ChangeTab ct;	//changeTab
	private UserManagementTab ut;	//userTab

	/************
	 * getters & setters
	 ************/
	public TableOption getTableName() {
		return tableName;
	}

	public void setTableName(TableOption tableName) {
		this.tableName = tableName;
	}
	public void setCursor(TableCursor cursor) {
		this.cursor=cursor;
	}

	public void setTable(Table table2) {
		this.table=table2;				
	}
	public void setChangeTab(ChangeTab changeTabTest){
		this.ct = changeTabTest;
		this.tableName=ChangeTab.getTableOption();
	}
	public void setUserTab(UserManagementTab ut){
		this.ut = ut;
		this.tableName=TableOption.USERS;
	}

	
	@Override
	/**
	 * checks if the pressed key is DEL. if so, calls the DeleteThread.
	 */
	public void keyPressed(KeyEvent arg0) {
		//check if user clicked ctrl-a to select all rows in table
		if(((arg0.stateMask & SWT.CTRL) == SWT.CTRL) && (arg0.keyCode == 'a'))
		{
			table.selectAll();
		}
		//if the user didn't hit the DELETE key  - return
		if (arg0.keyCode!=127)
			return;
		cursor.setVisible(true);
		BusyIndicator.showWhile(table.getDisplay(), new DeleteThread(table,arg0,ct,ut,table.getDisplay(),table.getShell()));
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}
}
